<?php
require_once(dirname(__FILE__)."/constants.php");
require_once(DB_SESSION_DIR_PATH."/xml2database.php");

function GetDocumentObject($file_name)
{
	$doc = new DOMDocument();
	$doc->load($file_name);
	//$doc = DOMDoucment::load($file_name);
	return $doc;
}

function GetChildDOMNodesArrayByType($node, $node_type)
{
	if (gettype($node) != gettype(new DOMNode()))
	{
		echo 'Wrong type of parameter $node. DOMNode is expected.';
		return NULL;
	}
	
	$retArray = array();
	$children = $node->childNodes;
	foreach ($children as $childNode)
	{
		if ($childNode->nodeType == $node_type)
		{
			$retArray[] = $childNode;
		}
	}
	return $retArray;
}

function GetDOMNodesByTagName($nodes, $tag_name)
{
	$ret = array();
	foreach ($nodes as $node)
	{
		if ($node->nodeType == XML_ELEMENT_NODE)
		{
			if (!strcmp($node->tagName, $tag_name))
			{
				$ret[] = $node;
			}
		}
	}
	return $ret;
}
function GetDataQueryConfig($file_name, $alias = null)
{
	$doc = GetDocumentObject($file_name);
	$host_node = $doc->documentElement;
	
	// Get host configuration
	if ($host_node->nodeType != XML_ELEMENT_NODE || $host_node->tagName != 'host')
	{
		echo 'No required host defined in the configuration from file '.$file_name;
		return NULL;
	}

	$host_name = $host_node->getAttribute('name');
	$usr_name = $host_node->getAttribute('username');
	$pwd = $host_node->getAttribute('password');
	if (!strcmp($host_name, ''))
	{
		echo 'The host name must be specified in the configuration from file '.$file_name;
		return NULL;
	}

	// Get database configuration
	$db_array = GetChildDOMNodesArrayByType($host_node, XML_ELEMENT_NODE);
	if ($db_array == NULL || count($db_array) == 0 || count($db_array) > 1)
	{
		echo 'No required database or more than one database defined in the configuration from file '.$file_name;
		return NULL;
	}
	$db_node = $db_array[0];
	$db_name = $db_node->getAttribute('name');
	if (!strcmp($db_name, ''))
	{
		echo 'The database name must be specified in the configuration from file '.$file_name;
		return NULL;
	}

	// Get table configuration
	$table_array = GetChildDOMNodesArrayByType($db_node, XML_ELEMENT_NODE);
	if ($table_array == NULL || count($table_array) == 0 || count($table_array) > 1)
	{
		echo 'No required table or more than one table defined in the configuration from file '.$file_name;
		return NULL;
	}

	$table = $table_array[0];
	$tbl_name = $table->getAttribute('name');
	if (!strcmp($tbl_name, ''))
	{
		echo 'Table name must be specified in the configuration from file '.$file_name;
		return NULL;
	}

	// Get columns configuration
	$child_array = GetChildDOMNodesArrayByType($table, XML_ELEMENT_NODE);
	$col_array = GetDOMNodesByTagName($child_array, 'column');
	if ($col_array == NULL || count($col_array) == 0)
	{
		echo 'No required columns defined in the configuration from file '.$file_name;
	}

	$col_name = array();
	foreach ($col_array as $col)
	{
		$col_name[] = $col->getAttribute('name');
	}

	$config = new DbMap();
	$config->setHost($host_name);
	$config->setUser($usr_name);
	$config->setPassword($pwd);
	$config->setDatabase($db_name);
	$config->setTable($tbl_name);
	$config->setColumns($col_name);
	$config->setAlias($alias);
	return $config;
}
?>